package com.miplus.ccrm.core.service.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.core.controller.vo.strategy.TaskLogReqVo;
import com.miplus.ccrm.core.controller.vo.strategy.TaskLogResVo;
import com.miplus.ccrm.core.pojo.CfgRuleTask;
import com.miplus.ccrm.core.pojo.CfgRuleTaskSteps;
import com.miplus.ccrm.core.pojo.LogXxlJob;
import com.miplus.ccrm.core.service.utils.LogXxlJobService;
import com.miplus.ccrm.core.mapper.LogXxlJobMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author wangzl
* @description 针对表【log_xxl_job】的数据库操作Service实现
* @createDate 2024-11-03 16:07:46
*/
@Service
public class LogXxlJobServiceImpl extends MPJBaseServiceImpl<LogXxlJobMapper, LogXxlJob>
    implements LogXxlJobService{

    @Override
    public IPage<TaskLogResVo> getTasJobLog(TaskLogReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(),paras.getPageSize()),
                TaskLogResVo.class,
                new MPJLambdaWrapper<LogXxlJob>()
                        .leftJoin(CfgRuleTask.class, CfgRuleTask::getRuleTaskId, LogXxlJob::getRuleTaskId)
                        .leftJoin(CfgRuleTaskSteps.class,"cts",CfgRuleTaskSteps::getRuleStepId,LogXxlJob::getRuleStepId)
                        .selectAll(LogXxlJob.class)
                        .selectAs(CfgRuleTask::getRuleTaskName,TaskLogResVo::getRuleTaskName)
                        .selectAs(CfgRuleTaskSteps::getRuleStepName,TaskLogResVo::getRuleStepName)
                        .selectAs("fun_getnames('RULE.LOGTYPE',t.op_type)", TaskLogResVo::getOpTypeDesc)
                        .selectAs("fun_getnames('RULE.STEP.ACTIONCODE',cts.action_code)",TaskLogResVo::getActionTypeDesc)
                        .eq(ObjectUtils.isNotNull(paras.getTaskId()),LogXxlJob::getRuleTaskId,paras.getTaskId())
                        .ge(StringUtils.isNotEmpty(paras.getStartDate()),LogXxlJob::getExecEndTime,paras.getStartDate())
                        .le(StringUtils.isNotEmpty(paras.getEndDate()),LogXxlJob::getExecEndTime,paras.getEndDate())
                        .orderByAsc(LogXxlJob::getLogId)
        );
    }
    @Override
    public void log(LogXxlJob logXxlJob, Integer op,
                    Integer planCount,Integer okCount, Integer status, String grpId,
                    String msg)
    {
        LogXxlJob logXxlJob1 = new LogXxlJob();
        BeanUtils.copyProperties(logXxlJob,logXxlJob1);
        logXxlJob1.setOpType(op);
        logXxlJob1.setExecStatus(status);
        logXxlJob1.setReadCounts(planCount);
        logXxlJob1.setWriteCounts(okCount);
        logXxlJob1.setGrpId(grpId);
        logXxlJob1.setExecMsg(msg);
        logXxlJob1.setExecEndTime(new Date());
        save(logXxlJob1);
    }
    @Override
    public void log(LogXxlJob logXxlJob, Integer op,
                    Integer planCount,Integer okCount, Integer status, String grpId,
                    String msg,Integer isManual)
    {
        LogXxlJob logXxlJob1 = new LogXxlJob();
        BeanUtils.copyProperties(logXxlJob,logXxlJob1);
        logXxlJob1.setOpType(op);
        logXxlJob1.setExecStatus(status);
        logXxlJob1.setReadCounts(planCount);
        logXxlJob1.setWriteCounts(okCount);
        logXxlJob1.setGrpId(grpId);
        logXxlJob1.setExecMsg(msg);
        logXxlJob1.setExecEndTime(new Date());
        save(logXxlJob1);
    }
}




